package hunglv4.vnu;

import java.util.ArrayList;
import java.util.Random;

import android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class MainActivity extends Activity implements OnClickListener {
	private Button generateBtn, mergeBtn, heapBtn, quickBtn;
	private TextView inputDisplay;
	private EditText inputN;

	private ArrayList<Integer> inputArray;

	public static String INTENT_VALUE_KEY = "intent_values";
	public static String BUNDLE_VALUE_KEY = "bundle_values";
	public static String BUNDLE_TYPE_KEY = "type";
	
	public static int QUICK_SORT = 1, MERGER_SORT = 2, HEAP_SORT = 3;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		generateBtn = (Button) findViewById(R.id.generateBtn);
		mergeBtn = (Button) findViewById(R.id.mergeBtn);
		heapBtn = (Button) findViewById(R.id.heapBtn);
		quickBtn = (Button) findViewById(R.id.quickBtn);
		inputN = (EditText)findViewById(R.id.inputN);
		inputDisplay = (TextView) findViewById(R.id.inputDisplay);

		generateBtn.setOnClickListener(this);
		mergeBtn.setOnClickListener(this);
		heapBtn.setOnClickListener(this);
		quickBtn.setOnClickListener(this);

		inputArray = new ArrayList<Integer>();
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.activity_main, menu);
		return true;
	}
	
	public void onClick(View v) {
		String sizeStr = inputN.getText().toString();
		if(sizeStr.equals("")) return;
		int size = Integer.valueOf(sizeStr);
		
		Intent intent = new Intent(MainActivity.this, DisplayActivity.class);
		Bundle bundle = new Bundle();
		
		switch (v.getId()) {
		case R.id.generateBtn:
			generateInputArray(size);
			display(inputArray, inputDisplay);
			break;
		case R.id.mergeBtn:
			if(inputArray.size() == 0){
				displayWarning();
				return;
			}
			bundle.putIntegerArrayList(BUNDLE_VALUE_KEY, inputArray);
			bundle.putInt(BUNDLE_TYPE_KEY, MERGER_SORT);
			intent.putExtra(BUNDLE_VALUE_KEY, bundle);
			startActivity(intent);
			break;
		case R.id.heapBtn:
			if(inputArray.size() == 0){
				displayWarning();
				return;
			}
			bundle.putIntegerArrayList(BUNDLE_VALUE_KEY, inputArray);
			bundle.putInt(BUNDLE_TYPE_KEY, HEAP_SORT);
			intent.putExtra(BUNDLE_VALUE_KEY, bundle);
			startActivity(intent);
			break;
		case R.id.quickBtn:
			if(inputArray.size() == 0){
				displayWarning();
				return;
			}
			bundle.putIntegerArrayList(BUNDLE_VALUE_KEY, inputArray);
			bundle.putInt(BUNDLE_TYPE_KEY, QUICK_SORT);
			intent.putExtra(BUNDLE_VALUE_KEY, bundle);
			startActivity(intent);
			break;
		default:
			return;
		}
	}

	private void generateInputArray(int size) {
		if (size <= 0)
			return;

		inputArray.clear();

		Random random = new Random();

		for (int i = 0; i < size; i++) {
			int randomNumber = random.nextInt(size * 10);
			inputArray.add(randomNumber);
		}
	}

	private void display(ArrayList<Integer> inputArray, TextView inputDisplay) {
		if (inputArray.size() == 0)
			return;
		inputDisplay.setText(inputArray.toString());
	}
	
	private void displayWarning(){
		AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
		builder.setTitle("Sorting Algorithm");
		builder.setMessage("You have no input array! Please click in Generate N Array button to make one!");
		builder.setNeutralButton("OK", new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialog, int which) {
				return;
			}
		});
		builder.show();
	}
}
